home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / CIconButton CDEF 1.0 / Sample Code / CIconButton CDemo.c next >
Encoding:
C/C++ Source or Header  |  1993-10-15  |  5.4 KB  |  234 lines  |  [TEXT/KAHL]

  1. /************
  2.  
  3.     Sample program for using the CIconButton CDEF 1.0
  4.     by Ramon M. Felciano
  5.     felciano@camis.stanford.edu
  6.     
  7.     ©1993 Ramon M. Felciano, All Rights Reserved
  8.         
  9. ********/
  10. #include "CIconButton.CLib.h"
  11.  
  12. #define    kCIconButtonCDEFID    20
  13. #define kButtonProc    (16 * kCIconButtonCDEFID)
  14.  
  15. #define    kSampleMain 650
  16. #define    kSampleShell 3210
  17. #define kSampleAlt 700
  18.  
  19. #define atLeft 0x08
  20. #define atRight 0x0C
  21. #define atTop 0x02
  22. #define atBottom 0x03
  23.  
  24.  
  25.  
  26. /****
  27.  *
  28.  * InitMacintosh()
  29.  *
  30.  ****/
  31.  
  32. void InitMacintosh(void)
  33.  
  34. {
  35.     MaxApplZone();
  36.     
  37.     InitGraf(&thePort);
  38.     InitFonts();
  39.     FlushEvents(everyEvent, 0);
  40.     InitWindows();
  41.     InitMenus();
  42.     TEInit();
  43.     InitDialogs(0L);
  44.     InitCursor();
  45.  
  46. }
  47. /* end InitMacintosh */
  48.  
  49.  
  50.  
  51. unsigned short RangedRdm( unsigned short min, unsigned short max )
  52. /* assume that min is less than max */
  53. {
  54.     unsigned    qdRdm;    /* treat return value as 0-65536 */
  55.     long    range, t;
  56.  
  57.     qdRdm = Random();
  58.     range = max - min;
  59.     t = (qdRdm * range) / 65536;     /* now 0 <= t <= range */
  60.     return( t+min );
  61. }
  62.  
  63.  
  64.  
  65. void doContentClick (WindowPtr theWindow, EventRecord theEvent)
  66. {
  67.     Point mouse;
  68.     ControlHandle control;
  69.     int checkBox;
  70.     int part;
  71.  
  72.     SetPort(theWindow);
  73.     mouse = theEvent.where;
  74.     GlobalToLocal(&mouse);
  75.     part = FindControl(mouse, theWindow, &control);
  76.     if (( part > 0 ) && (control != nil)) {
  77.         if (TrackControl(control, mouse, nil) != 0 )
  78.             {
  79.                 checkBox = GetCtlValue(control);
  80.                 checkBox = 1 - checkBox;
  81.                 SetCtlValue(control, checkBox);
  82.                 
  83.                 switch (RangedRdm(1,5)) {
  84.                     case 1:
  85.                         CIB_SetTitleOrientation(control, atLeft);
  86.                         break;
  87.                         
  88.                     case 2:
  89.                         CIB_SetTitleOrientation(control, atRight);
  90.                         break;
  91.                         
  92.                     case 3:
  93.                         CIB_SetTitleOrientation(control, atTop);
  94.                         break;
  95.                         
  96.                     case 4:
  97.                         CIB_SetTitleOrientation(control, atBottom);
  98.                         break;
  99.                     }
  100.                 InvalRect(&(**control).contrlRect);
  101.             };
  102.         };
  103. }
  104.  
  105.  
  106. void DrawMyWindow(WindowPtr whichWindow, ControlHandle btn)
  107. {
  108.         Rect textRect;
  109.         Str255    theCopyright;
  110.  
  111.         FillRect(&whichWindow->portRect, gray);
  112.         
  113.         textRect = whichWindow->portRect;
  114.         InsetRect(&textRect, 5, 5);
  115.         textRect.bottom = textRect.top + 44;
  116.         FillRect(&textRect, white);
  117.  
  118.         CIB_GetCopyright(btn, theCopyright);
  119.         MoveTo(12,18);
  120.         DrawString(theCopyright);
  121.         
  122.         MoveTo(12,30);
  123.         DrawString("\pClick on the button to randomly change the title orientation.");
  124.         
  125.         MoveTo(12,42);
  126.         DrawString("\pClick the close box or hit any key to quit.");
  127.         
  128.         UpdateControls(whichWindow, whichWindow->visRgn);
  129. }
  130.  
  131. /*****
  132.  * doSimpleWindow()
  133.  *
  134.  *    Show a simple window with a single control
  135.  *
  136.  *****/
  137.  
  138.     void doSimpleWindow()
  139.  
  140.     {    
  141.             Boolean Done;
  142.             WindowPtr theWindow;
  143.             int windowPart;
  144.             CWindowRecord wRecord;
  145.             Rect limitRect;
  146.             Rect rWindow;
  147.  
  148.             ControlHandle bPushButton;
  149.             Rect itemBox;
  150.  
  151.             EventRecord theEvent;
  152.             Point newWindowLoc;
  153.             GrafPtr savePort;
  154.             WindowPtr whichWindow;
  155.  
  156.             OSErr oe;
  157.  
  158.         bPushButton = nil;
  159.         Done = FALSE;
  160.         theWindow = nil;
  161.         SetRect(&rWindow, 0, 0, 400, 150);
  162.         OffsetRect(&rWindow, 50, 50);
  163.         SetRect(&itemBox, 50, 50, 180, 120);
  164.         limitRect = screenBits.bounds;
  165.         theWindow = NewCWindow(nil, &rWindow, "\pCIconButton Demo", TRUE, documentProc, (WindowPtr)(-1), TRUE, 0);
  166.  
  167.         SetPort(theWindow);
  168.         BackPat(gray);
  169.         TextFont(geneva);
  170.         TextSize(9);
  171.  
  172.         ShowWindow(theWindow);
  173.         
  174.         bPushButton = NewControl(theWindow, &itemBox, "\pTesting", FALSE, kSampleAlt, kSampleMain, kSampleShell, kButtonProc, 0);
  175.         ShowControl(bPushButton);
  176.         
  177.         DrawMyWindow(theWindow,bPushButton);
  178.         
  179.         while (!Done) {
  180.             if (WaitNextEvent(everyEvent, &theEvent, 0, nil))
  181.       
  182.                 switch (theEvent.what) {
  183.                     case keyDown: 
  184.                         Done = TRUE;
  185.                         break;
  186.  
  187.                     case mouseDown: 
  188.                         windowPart = FindWindow(theEvent.where, &theWindow);
  189.                         switch (windowPart) {
  190.                             case inDrag: 
  191.                                  DragWindow(theWindow, theEvent.where, &limitRect);
  192.                                  newWindowLoc = topLeft(theWindow->portRect);
  193.                                  LocalToGlobal(&newWindowLoc);
  194.                                  MoveWindow(theWindow, newWindowLoc.h, newWindowLoc.v, TRUE);
  195.                                  break;
  196.  
  197.                             case inGoAway: 
  198.                                  Done = TrackGoAway(theWindow, theEvent.where);
  199.                                  break;
  200.                             
  201.                             case inContent: 
  202.                                  doContentClick(theWindow, theEvent);
  203.                                  break;
  204.                              }
  205.                         break;
  206.   
  207.                      case updateEvt: 
  208.                         GetPort(&savePort);
  209.                         whichWindow = (WindowPtr)(theEvent.message);
  210.                         BeginUpdate(whichWindow);
  211.                         DrawMyWindow(whichWindow, bPushButton);
  212.                          EndUpdate(whichWindow);
  213.                         SetPort(savePort);
  214.                         break;
  215.  
  216.                 }
  217.         };
  218.  
  219.         if ( theWindow != nil )
  220.             DisposeWindow(theWindow);
  221.     }
  222.  
  223.  
  224. main()
  225.  
  226. {
  227.  
  228.     InitMacintosh();
  229.  
  230.     doSimpleWindow();
  231.  
  232.     
  233. }
  234.